package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.Order;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;
import com.wholesale.utils.MyOrderListAdapter;

public class MyOrdersLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private Button inTowMonthButton;
	private Button outTowMonthButton;
	private MyOrderListAdapter listAdapter;
	private MyOrdersListAsync myOders;
	private ArrayList<Order> orders;
	private RelativeLayout loadingLayout;
	private boolean currentScope = true;

	// private
	// 构造函数
	public MyOrdersLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.under3, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("My Orders");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);

		bodyList.setSelector(R.drawable.details_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);

		listAdapter = new MyOrderListAdapter(context, bodyList);
		bodyList.setAdapter(listAdapter);

		inTowMonthButton = (Button) baseView
				.findViewById(R.id.under_low_to_high);
		inTowMonthButton.setText("within two months");
		inTowMonthButton.setOnClickListener(this);
		outTowMonthButton = (Button) baseView
				.findViewById(R.id.under_high_to_low);
		outTowMonthButton.setText("before two months");
		outTowMonthButton.setOnClickListener(this);
		myOders = new MyOrdersListAsync(context);
		myOders.execute(true);
	}

	// 用于其他页面调用
	public void refreshMyOrders() {
		myOders = new MyOrdersListAsync(context);
		myOders.execute(currentScope);
	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			int orderid = orders.get(arg2).getOrderId();
			Order order = orders.get(arg2);
			MyOrdersDetailLayout orderDetailLayout = new MyOrdersDetailLayout(
					context, viewFlipper, orderid, order);
			viewFlipper.addView(orderDetailLayout);
			viewFlipper.showNext();
		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.under_low_to_high:
			inTowMonthButton
					.setBackgroundResource(R.drawable.under_segment_left_on);
			outTowMonthButton
					.setBackgroundResource(R.drawable.under_segment_right_off);
			if (myOders != null) {
				myOders.cancel(true);
			}
			myOders = new MyOrdersListAsync(context);
			myOders.execute(true);
			currentScope = true;
			break;

		case R.id.under_high_to_low:
			inTowMonthButton
					.setBackgroundResource(R.drawable.under_segment_left_off);
			outTowMonthButton
					.setBackgroundResource(R.drawable.under_segment_right_on);
			if (myOders != null) {
				myOders.cancel(true);
			}
			myOders = new MyOrdersListAsync(context);
			myOders.execute(false);
			currentScope = false;
			break;

		default:
			break;
		}

	}

	class MyOrdersListAsync extends
			AsyncTask<Boolean, Integer, ArrayList<Order>> {
		ProgressDialog pdialog;

		public MyOrdersListAsync(Context context) {
			// pdialog = new ProgressDialog(context);
			// pdialog.setTitle("Loading...");
			// pdialog.setMessage("Please wait...");
			// pdialog.setCancelable(true);
			// pdialog.setButton("cancel", new DialogInterface.OnClickListener()
			// {
			// public void onClick(DialogInterface dialog, int i) {
			// dialog.cancel();
			// MyOrdersListAsync.this.cancel(true);
			// }
			// });
			// pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected ArrayList<Order> doInBackground(Boolean... params) {
			boolean flag = params[0];

			orders = WebServiceClient.getMyOrders(
					Constants.loginResponse.getUserId(), flag,
					Constants.loginResponse.getUserToken());
			if (orders == null) {
				return new ArrayList<Order>();
			} else {
				return orders;
			}
		}

		@Override
		protected void onPostExecute(ArrayList<Order> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
			// pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

}
